package com.example.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.widget.Button;
import android.widget.EditText;

public class Contact {
	private DaonSqliteHelper helper;
	private SQLiteDatabase db;

	// ------------------Create db and close db
	public Contact(Context context) {
		helper = new DaonSqliteHelper(context);
		db = helper.getWritableDatabase();
	}

	public void close() {
		helper.close();
		db.close();
	}
	
	public static abstract class phoneNumber implements BaseColumns{
		private static final String TABLE_NAME = "phonenumber";
		private static final String COLUNM_LINE = "number";
		private static final String DATA_TYPE = " TEXT";
		private static final String CREATE_QUERY = "CREATE TABLE "
				+ phoneNumber.TABLE_NAME + " (" + phoneNumber.COLUNM_LINE + user.DATA_TYPE+")";
	}

	public static abstract class user implements BaseColumns {
		private static final String TABLE_NAME = "user";
		private static final String COLUNM_LINE = "Line";
		private static final String COLUNM_NAME = "Username";
		private static final String COLUNM_PASS = "Pass";
		private static final String COLUNM_URLSERVER = "url";
		private static final String COLUNM_URLSERVER_LINK = "link";
		private static final String DATA_TYPE = " TEXT";
		private static final String CREATE_QUERY = "CREATE TABLE "
				+ user.TABLE_NAME + " (" + user.COLUNM_LINE + user.DATA_TYPE
				+ "," + user.COLUNM_NAME + user.DATA_TYPE + ","
				+ user.COLUNM_PASS + user.DATA_TYPE +","+ user.COLUNM_URLSERVER
				+ user.DATA_TYPE +","+ user.COLUNM_URLSERVER_LINK
				+ user.DATA_TYPE +")";
	}

	public static abstract class Name implements BaseColumns {
		private static final String TABLE_NAME = "name";
		private static final String COLUNM_ID = "KhID";
		private static final String COLUNM_MA = "Ma";
		private static final String COLUNM_TE = "Ten";
		private static final String COLUNM_DC = "DiaChi";
		private static final String COLUNM_DT = "DienThoai";
		private static final String COLUNM_TT = "TrangThai";
		private static final String DATA_TYPE = " TEXT";
		private static final String CREATE_QUERY = "CREATE TABLE "
				+ Name.TABLE_NAME + " (" + Name.COLUNM_ID + Name.DATA_TYPE
				+ "," + Name.COLUNM_MA + Name.DATA_TYPE + "," + Name.COLUNM_TE
				+ Name.DATA_TYPE + "," + Name.COLUNM_DC + Name.DATA_TYPE + ","
				+ Name.COLUNM_DT + Name.DATA_TYPE + "," + Name.COLUNM_TT
				+ Name.DATA_TYPE + ")";
	}

	public void insertUser(String line, String userName, String pass, String url, String link) {
		ContentValues values = new ContentValues();
		values.put(user.COLUNM_LINE, line);
		values.put(user.COLUNM_NAME, userName);
		values.put(user.COLUNM_PASS, pass);
		values.put(user.COLUNM_URLSERVER, url);
		values.put(user.COLUNM_URLSERVER_LINK, link);
		db.insert(user.TABLE_NAME, null, values);
	}
	
	public void insertPhone(String number){
		ContentValues values = new ContentValues();
		values.put(phoneNumber.COLUNM_LINE, number);
		db.insert(phoneNumber.TABLE_NAME, null, values);
	}
	

	public void insertName(String id, String ma, String ten, String diachi,
			String dienthoai, String trangthai) {
		ContentValues values = new ContentValues();
		values.put(Name.COLUNM_ID, id);
		values.put(Name.COLUNM_MA, ma);
		values.put(Name.COLUNM_TE, ten);
		values.put(Name.COLUNM_DC, diachi);
		values.put(Name.COLUNM_DT, dienthoai);
		values.put(Name.COLUNM_TT, trangthai);
		db.insert(Name.TABLE_NAME, null, values);
	}
	
	public void deleteNumber(String number){
		db.execSQL("DELETE FROM "+phoneNumber.TABLE_NAME+" WHERE "+phoneNumber.COLUNM_LINE+" ='"+number+"'");
	}

	public void deleteUser() {
		db.execSQL("DELETE FROM " + user.TABLE_NAME);
	}

	public void DeleteName() {
		db.execSQL("DELETE FROM " + Name.TABLE_NAME);
	}
	
	public Cursor selectPhone(){
		Cursor c = db.rawQuery("Select * from " + phoneNumber.TABLE_NAME, null);
		return c;
	}

	public Cursor selectUser() {
		Cursor c = db.rawQuery("Select * from " + user.TABLE_NAME, null);
		return c;
	}
	
	public Cursor selectCount() {
		Cursor c = db.rawQuery("select count(*) from "+ Name.TABLE_NAME, null);
		return c;
	}
	
	

	public Cursor SelectName(String number) {
		Cursor c = db.rawQuery("Select * from " + Name.TABLE_NAME
				+ " where DienThoai like '%" + number + "%'", null);
		return c;
	}

	public class DaonSqliteHelper extends SQLiteOpenHelper {

		public static final int DATABASE_VERSION = 1;
		public static final String DATABASE_NAME = "FeedReader.db";

		public DaonSqliteHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(Name.CREATE_QUERY);
			db.execSQL(user.CREATE_QUERY);
			db.execSQL(phoneNumber.CREATE_QUERY);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			onCreate(db);

		}
	}

}
